package app.netant.badbook.haxwen;

import org.jsoup.select.Elements;

import app.netant.NetAntBase;
import cn.lily.com.db.DBFactory;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.enviroment.Enviroment;
import cn.lily.com.lang.number.MathUtil;

public class HaxwenNetPage extends NetAntBase {

	public HaxwenNetPage(String netSite) {
		super(netSite);
	}
	
	public void analyse(){
		
		Elements activePart = document.select("#container #content .inner .details .item-con li .s2 a");
		if(activePart.size()>0){
			
			for(int i=0;i<activePart.size();i++){
				
				String href = activePart.get(i).attr("href");
				if(href.length()<=40){
					
					if(href.startsWith("/")){
						href = "https://www.haxwx11.com" + href;
					}
					
					Enviroment.getEnv().getLogBean().beginLog();
					
					System.out.println(href + "地址开始处理.........");
					
					HaxwenNetDetail detail = null;
					try {
						
						DataTable dataTable = this.getDBManager().executeQueryTable("select * from ${schema}.NET_HAXWEN_INFO where NOVEL_URL='"+href+"'");
						if(dataTable.next()){
							continue;
						}

						HaxwenEntity entity = new HaxwenEntity();
						entity.setNovelUrl(href);
						
						detail = new HaxwenNetDetail(href);		
						detail.setEntity(entity);
						
						detail.analyse();
						
					} catch (Exception e) {
						
						e.printStackTrace();
						
						DBFactory.getThreadDB().executeUpdate("insert into ${schema}.NET_HAXWEN_EXCEP (EXCEP_ID,EXCEP_URL,EXCEP_LEVEL) "
															+ "values ('"+System.currentTimeMillis()+""+MathUtil.random(3)+"','"+href+"','2')");
						
						continue;
						
					} finally {
						if(detail != null){
							detail.free();
						}
					}
					
					Enviroment.getEnv().getLogBean().endLog();
					
				}else{
					continue;
				}

			}
			
		}
		
	}
	
	public static void main(String[] args) {
		
		Enviroment.createEnv();
		Enviroment.getEnv().getLogBean().beginLog();
		
		HaxwenNetPage page = new HaxwenNetPage("https://www.haxwx11.com/xiaoshuo/1/1.htm");
		page.analyse();
	}

}
